import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Index from '../views/Index.vue'
import Goods from '../views/Goods.vue'
import Operation from '../views/Operation.vue'
import AddGoods from '../views/Goods/Add.vue'
import ListGoods from '../views/Goods/List.vue'
import Audit from '../views/Goods/Audit.vue'
import Recycle from '../views/Goods/Recycle.vue'
import Evaluate from '../views/Goods/Evaluate.vue'
import Classify from '../views/Goods/Classify.vue'
import AddClassify from '../views/Goods/Classify/AddClassify.vue'
import Below from '../views/Goods/Classify/Below.vue'
import Below3 from '../views/Goods/Classify/Below3.vue'
import Parameter from '../views/Goods/Parameter.vue'
import Parm from '../views/Goods/Parameter/Parm.vue'
import Spec from '../views/Goods/Parameter/Spec.vue'
import AddParm from '../views/Goods/Parameter/AddParm.vue'
import AddSpec from '../views/Goods/Parameter/AddSpec.vue'
import Brand from '../views/Goods/Brand.vue'
import BrandAdd from '../views/Goods/Brand/Add.vue'
import Library from '../views/Goods/Library.vue'
import ListOperation from '../views/Operation/List.vue'
import TimeOperation from '../views/Operation/Time.vue'
import Advertising from '../views/Operation/Advertising.vue'
import Activity from '../views/Operation/Activity.vue'
// 添加商品
import AddStepOne from '../views/Goods/AddGoodsStep/AddStepOne.vue'
import AddStepTwo from '../views/Goods/AddGoodsStep/AddStepTwo.vue'
import AddStepThree from '../views/Goods/AddGoodsStep/AddStepThree.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: Index
      },
      {
        path: '/goods',
        component: Goods,
        children: [
          { path: '/goods/list', component: ListGoods },
          {
            path: '/goods/add',
            component: AddGoods,
            children: [
              { path: '/goods/add/step1', component: AddStepOne },
              { path: '/goods/add', redirect: '/goods/add/step1' },
              { path: '/goods/add/step2/:id', component: AddStepTwo },
              { path: '/goods/add/step3', component: AddStepThree }
            ]
          },
          { path: '/goods/audit', component: Audit },
          { path: '/goods/recycle', component: Recycle },
          { path: '/goods/evaluate', component: Evaluate },
          { path: '/goods/classify', component: Classify },
          { path: '/goods/classify/add', component: AddClassify },
          { path: '/goods/classify/below', component: Below },
          { path: '/goods/classify/below3', component: Below3 },
          { path: '/goods/parameter', component: Parameter },
          { path: '/goods/parameter/parm', component: Parm },
          { path: '/goods/parameter/addparm', component: AddParm },
          { path: '/goods/parameter/addspec', component: AddSpec },
          { path: '/goods/parameter/spec', component: Spec },
          { path: '/goods/parameter', component: Parameter },
          { path: '/goods/brand', component: Brand },
          { path: '/goods/brand/add', component: BrandAdd },
          { path: '/goods/library', component: Library }
        ]
      },
      {
        path: '/operation',
        component: Operation,
        children: [
          { path: '/operation/list', component: ListOperation },
          { path: '/operation/time', component: TimeOperation },
          { path: '/operation/advertising', component: Advertising },
          { path: '/operation/activity', component: Activity }
        ]
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to表示将要访问的路径
  // from 表示从哪个路径跳转过来
  // next 是一个函数 表示放行
  // next 表示放行 next('/login') 表示强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token 则强制跳转到登录页面
  if (!tokenStr) return next('/login')
  next()
})
export default router
